package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;

public class CbNewsAnalyse implements AnalyseNews {

/**
 * http://www.cb.com.cn/person/2014_0117/1034202.html
 * http://news.cb.com.cn/company_11_16345_1.html
 */
	private static final String[] regex={
		"http://.*.cb.com.cn/[a-z]*/[0-9]{4}_[0-9]{4}/[0-9]*.html",
		"http://news.cb.com.cn/[a-z]*_[0-9]*_[0-9]*_[0-9]*.html",
		"http://news.cb.com.cn/.*/[a-z]*_[0-9]*_[0-9]*_[0-9]*.html",
	};

	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String url=urlMeta.getUrl();
//		NewsMeta news=new ParseNewsUtils().parse(urlMeta);
		NewsMeta news=new NewsMeta();
		String htmltxt=urlMeta.getHtml();
		Document doc=Jsoup.parse(htmltxt);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		title=doc.select("a.t30_g").text();
		if(StringUtils.isBlank(title)){
			title=doc.select("span.t30_g").text();
			if(StringUtils.isBlank(title)){
				title=doc.select("div.c_listtext > h4").text();
			}
		}
		if(StringUtils.isBlank(title)){
			title=doc.select(".contenttit").text();
		}
		content=HtmlCleaner.getContentHtml(url ,doc.select("div#Article"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url ,doc.select("div.text"));
		}
		if(StringUtils.isBlank(content)){
			content=doc.select("meta[name=description]").attr("content");
		}

		author=JSoupUtils.matchAuthor(doc, "来源:");

		date=DateUtils.matchDate(doc.select("div.art_nav").text());
		if(date == null){
			date=DateUtils.matchDate(doc.select("div.list_p").text());
			if(date == null){
				date=DateUtils.matchDate(doc.select("div.c_listtext h2").text());
			}
		}
		news.setUrl(url);
		news.setTitle(title);
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);
		if (StringUtils.isBlank(news.getTitle())||news.getDate() == null) {
		}
		return news;
	}

	public static void main(String[] args) {
		String url="http://news.cb.com.cn/zhengquanjijin/2016_1107/1171466.html";
		CbNewsAnalyse test=new CbNewsAnalyse();
		url=UrlArgumentTop.FromatUrl(url);
		System.out.println(test.isDetailPage(url));
		System.out.println(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=test.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
